// QQ表情插件
(function($){  
	$.fn.qqFace = function(options){
		var defaults = {
			id : 'facebox',
			path : 'face/',
			assign : 'content',
			tip : 'em_'
		};
		var option = $.extend(defaults, options);
		var assign = $('#'+option.assign);
		var id = option.id;
		var path = option.path;
		var tip = option.tip;
		
		if(assign.length<=0){
			alert('缺少表情赋值对象。');
			return false;
		}
		
		$(this).click(function(e){
			var t = $(this).attr("isopen");
			if($(this).attr("isopen") == "true"){
				$('#'+id).hide();
				$('#'+id).remove();
				$(".main-content").css("bottom","58px");
				var t = $("#content")[0];
				$("#content")[0].scrollTop = $("#content")[0].scrollHeight;
				$(this).attr("isopen","false");
			}else{
				var strFace, labFace;
				if($('#'+id).length<=0){
					strFace = '<table id=' + id + ' border="0" cellspacing="0" cellpadding="0"><tr>';
					for(var i=1; i<=64; i++){
						labFace = '['+tip+i+']';
						strFace += '<td><img src="'+ ctx + '/static/chat/'+path+i+'.gif" onclick="$(\'#'+option.assign+'\').setCaret();$(\'#'+option.assign+'\').insertAtCaret(\'' + labFace + '\');" /></td>';
						if( i % 9 == 0 ) strFace += '</tr><tr>';
					}
					strFace += '</tr><tr class="face-del"><td><span class="iconfa-circle-arrow-left" onClick="delFace();"></span></td></tr></table>';
				}
				$(this).parents(".send-box").append(strFace);
				var offset = $(this).position();
				var top = offset.top + $(this).outerHeight();
				$('#'+id).show();
				$(".main-content").css("bottom","356px");
				var t = $("#content")[0];
				$("#content")[0].scrollTop = $("#content")[0].scrollHeight;
				e.stopPropagation();
				$(this).attr("isopen","true");
			}
			
		});

		$("#message-list").click(function(){
			$('#'+id).hide();
			$('#'+id).remove();
			$(".main-content").css("bottom","58px");
			$("#content")[0].scrollTop = $("#content")[0].scrollHeight;
			$("#toggle-emoji").attr("isopen","false");	
		});
	};

})(jQuery);

function delFace(){
	var msg = $("#msg").val();
	var flag = false;
	if(msg[msg.length-1] == "]"){
		for(var i = msg.length-1; i >=0 ;i--){
			if(msg[i] == "["){
				msg = msg.substring(0,i);
				flag = true;
				break;
			}
		}
		if(flag == false){
			msg = msg.substring(0,msg.length-1);
		}
	}else{
		msg = msg.substring(0,msg.length-1);
	}
	$("#msg").val(msg);
}

jQuery.extend({ 
unselectContents: function(){ 
	if(window.getSelection) 
		window.getSelection().removeAllRanges(); 
	else if(document.selection) 
		document.selection.empty(); 
	} 
}); 
jQuery.fn.extend({ 
	selectContents: function(){ 
		$(this).each(function(i){ 
			var node = this; 
			var selection, range, doc, win; 
			if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined'){ 
				range = doc.createRange(); 
				range.selectNode(node); 
				if(i == 0){ 
					selection.removeAllRanges(); 
				} 
				selection.addRange(range); 
			} else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())){ 
				range.moveToElementText(node); 
				range.select(); 
			} 
		}); 
	}, 

	setCaret: function(){ 
		//if(!$.browser.msie) return; 
		/*var initSetCaret = function(){ 
			var textObj = $(this).get(0); 
			textObj.caretPos = document.selection.createRange().duplicate(); 
		}; 
		$(this).click(initSetCaret).select(initSetCaret).keyup(initSetCaret); */
	}, 

	insertAtCaret: function(textFeildValue){ 
		var textObj = $(this).get(0); 
		if(document.all && textObj.createTextRange && textObj.caretPos){ 
			var caretPos=textObj.caretPos; 
			caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == '' ? 
			textFeildValue+'' : textFeildValue; 
		} else if(textObj.setSelectionRange){ 
			var rangeStart=textObj.selectionStart; 
			var rangeEnd=textObj.selectionEnd; 
			var tempStr1=textObj.value.substring(0,rangeStart); 
			var tempStr2=textObj.value.substring(rangeEnd); 
			textObj.value=tempStr1+textFeildValue+tempStr2; 
			textObj.focus(); 
			var len=textFeildValue.length; 
			textObj.setSelectionRange(rangeStart+len,rangeStart+len); 
			textObj.blur(); 
		}else{ 
			textObj.value+=textFeildValue; 
		} 
	} 
});